// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const db = uniCloud.database();
module.exports = {
	async homeData() {
		//  获取轮播图数据
		let bannerRes = db.collection('banner').orderBy('sort', 'asc').where({
			status: true
		}).get()
		// 获取商品分类
		let adverRes = db.collection('adver').orderBy('sort', 'asc').where({
			is_show: true
		}).get()
		
		let cateRes = db.collection('category').orderBy('sort', 'asc').where({
			is_index_show: true
		}).get()
		return {
			banners: (await bannerRes).data,
			advers: (await adverRes).data,
			categories:(await cateRes).data,
		}
	},
	banner() {
		return db.collection('banner').orderBy('sort', 'asc').where({
			status: true
		}).get()
	},
	categories() {
		return db.collection('categories').orderBy('sort', 'asc').where({
			is_index_show: true
		}).get()
	},
	goods({
		isNew = false,
		isHot = false,
		Commemt = false,
		Sales = false
	}) {
		let goods = db.collection('goods')
		let where = {}

		if (isNew) where.is_new = true
		if (isHot) where.is_hot = true
		goods = goods.where(where)

		if (Commemt) goods = goods.orderBy('comment_count', 'desc')
		if (Sales) goods = goods.orderBy('month_sell_count', 'desc')
		return goods.get()
	}
}
